// pages/register/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: "",
        name: "",
        sex: "女",
        activeTab: 1,
        checked: true,
        showAgreementPopup: false, //隐私协议popup
        showBindPhonePopup: false,
        openid: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            openid: options.openid,
            phone: options.phone
        })
        if (!this.data.phone) {
            this.setData({
                showBindPhonePopup: true
            })
        }
    },
    /** 注册 */
    registerFun(e) {
        const {
            type
        } = e.currentTarget.dataset
        let if_vip;
        if (type) {
            if_vip = true
        } else {
            if_vip = false
        }
        if (this.data.name.trim() == '') {
            wx.showModal({
                showCancel:false,
                content: "请输入称呼",
              })
            return
        }
        http.putHttp(api.updateUserInfo + this.data.phone, {
                sex: this.data.sex,
                name: this.data.name,
                if_vip:if_vip
            },
            (result) => {
                if (result.code == 0) {
                    if(if_vip){
                        wx.navigateTo({
                          url: '/pages/activationInfo/index?phone='+this.data.phone,
                        })
                    }else{
                      wx.switchTab({
                        url: '/pages/index/index',
                    })  
                    }
                    wx.showToast({
                        title: '注册成功',
                      })
                }else{
                    wx.showToast({
                        icon:"error",
                      title: '请勿输入非法字符',
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )

    },
    getPhoneFun(e) {
        if (e.detail.errMsg == 'getPhoneNumber:ok') {
            const {
                code
            } = e.detail
            http.postHttp(api.getUserPhone, {
                    code: code,
                    openid: this.data.openid
                },
                (res) => {
                    if (res.code == 0) {
                        this.setData({
                            phone: res.data,
                            showBindPhonePopup: false
                        })
                    } else {
                       
                        wx.showModal({
                            showCancel:false,
                            content: res.message,
                          })
                    }
                },
                (err) => {
                    console.log(err);
                },
            )
        }

    },
    onCloseAgreementPopup() {
        this.setData({
            showAgreementPopup: false
        })
    },
    onChangeCheckBox(event) {
        this.setData({
            checked: event.detail,
        });
    },
    /** 点击用户隐私协议触发 */
    tapAgreementFun() {
        this.setData({
            showAgreementPopup: true
        })
    },
    iSeeQueryFun(e) {
        this.setData({
            checked: e.detail.checked,
            showAgreementPopup: e.detail.showAgreementPopup
        });
    },

    onChangeSex(event) {
        this.setData({
            radio: event.detail,
        });
    },
    changeUserTab() {
        this.setData({
            activeTab: 1
        })
    },
    changePayUserTab() {
        this.setData({
            activeTab: 2
        })
    },
    /** 点击激活权益按钮触发 */
    changeActivationFun() {
        wx.navigateTo({
            url: '/pages/activationInfo/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})